﻿using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace JiveMessenger.CustomControls
{
    public sealed partial class PrivateMessageControl : UserControl
    {
        ~PrivateMessageControl()
        {
            Debug.WriteLine("PrivateMessageControl destructed");
        }

        public PrivateMessageControl()
        {
            this.InitializeComponent();

        }

        private void JChatControl_Loaded(object sender, RoutedEventArgs e)
        {
            DisplayInformation.GetForCurrentView().OrientationChanged += GroupChatControl_OrientationChanged;
            Messenger.Default.Register<NotificationMessage>(this, async msg =>
            {
                ViewModelChat vm = this.DataContext as ViewModelChat;
                switch (msg.Notification)
                {
                    case "ScrollToEnd":
                        if (vm != null && (msg.Target != null && msg.Target.ToString() == vm.ID) || msg.Sender == vm)
                            this.ChatControl.SetScrollBar(true, -1, false);
                        break;
                    case "BeginMessageQueue":
                        if (vm != null && (msg.Target != null && msg.Target.ToString() == vm.ID) || msg.Sender == vm)
                        {
                            this.ChatControl.Opacity = 0;
                        }
                        break;
                    case "EndMessageQueue":
                        if (vm != null && (msg.Target != null && msg.Target.ToString() == vm.ID) || msg.Sender == vm)
                        {
                            await Task.Delay(150);
                            this.ChatControl.SetScrollBar(true, -1, false);
                            this.ChatControl.Animate(0, 1, "Opacity", 300, 350);
                        }
                        break;
                }
            });
        }

        private void JChatControl_Unloaded(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Unregister(this);
            DisplayInformation.GetForCurrentView().OrientationChanged -= GroupChatControl_OrientationChanged;
            Debug.WriteLine("PrivateMessageControl unloaded");
        }

        void GroupChatControl_OrientationChanged(DisplayInformation sender, object args)
        {
            if (sender.CurrentOrientation == DisplayOrientations.Landscape || sender.CurrentOrientation == DisplayOrientations.LandscapeFlipped)
                this.ChatControl.MaxLinesPerMessage = 8;
            else
                this.ChatControl.MaxLinesPerMessage = 12;
        }

        private async void JChatControl_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
        {
            if (args.NewValue != null)
            {
                if (args.NewValue is ViewModelChat)
                {
                    ViewModelChat chatVm = (ViewModelChat)args.NewValue;
                    this.ChatControl.FadeOutChatLog(0);
                    if (!this.ChatControl.IsLoaded)
                    {
                        RoutedEventHandler handler = null;
                        handler = delegate(object s, RoutedEventArgs e)
                        {
                            this.ChatControl.Loaded -= handler;
                            this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                            {
                                await Task.Delay(50);
                                ScrollChatcontrol(chatVm);
                                this.ChatControl.FadeInChatLog(300, 500);
                            });
                        };
                        this.ChatControl.Loaded += handler;
                    }
                    else
                    {
                        await Task.Delay(50);
                        ScrollChatcontrol(chatVm);
                        this.ChatControl.FadeInChatLog(300, 500);

                    }

                }
            }
        }
        private void ScrollChatcontrol(ViewModelChat chatVm)
        {
            if (!chatVm.IsScrolledToEnd && chatVm.ScrollOffset > 0)
                this.ChatControl.SetScrollBar(false, chatVm.ScrollOffset, false);
            else
                this.ChatControl.SetScrollBar(true, -1, false);
        }
    }
}
